package com.srwl.mytx.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.google.android.material.snackbar.Snackbar;
import com.srwl.mytx.R;

import java.util.zip.Inflater;

/**
 * 搜索view
 */
public class SearchView extends ConstraintLayout implements View.OnClickListener {
    private Context context;
    public EditText et_content;
    private ImageView iv_contentClear;
    public TextView tv_search;
    private InteractionListener listener;


    public SearchView(Context context) {
        super(context);
        this.context = context;
        init(null, 0);
    }

    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs, 0);
    }

    public SearchView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        init(attrs, defStyle);
    }


    private void initView(Context context, AttributeSet attrs) {
        TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.SearchView);//获取配置属性
        String hit = tArray.getString(R.styleable.SearchView_hint);
        tArray.recycle();

    }

    private void init(AttributeSet attrs, int defStyle) {
        inflate(context, R.layout.my_search_view, this);
        et_content = findViewById(R.id.et_content);
        TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.SearchView);//获取配置属性
        String hit = tArray.getString(R.styleable.SearchView_hint);
        tArray.recycle();
        et_content.setHint(hit);
        iv_contentClear = findViewById(R.id.iv_content_clear);
        tv_search = findViewById(R.id.tv_search);
        et_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (listener != null) {
                    listener.onTextChange(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 0) {
                    iv_contentClear.setVisibility(VISIBLE);
                } else {
                    iv_contentClear.setVisibility(INVISIBLE);
                }
            }
        });
        iv_contentClear.setOnClickListener(this);
        tv_search.setOnClickListener(this);

        et_content.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    if (listener != null) {
                        listener.onSearch(et_content.getText().toString());
                    }
                    return true;
                }

                return false;
            }
        });
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_content_clear:
                et_content.setText("");
                break;
            case R.id.tv_search:
                String content = et_content.getText().toString();
                /*if (TextUtils.isEmpty(content)) {
                    Toast.makeText(context, "搜索内容不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }*/
                if (listener != null) {
                    listener.onSearch(content);
                }

                break;
        }
    }


    public void setListener(InteractionListener listener) {
        this.listener = listener;
    }

    public interface InteractionListener {
        void onSearch(String content);

        void onTextChange(String content);
    }

}
